import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/ins_task_detail_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/intelligent_inspection_page/inspection_list_page/inspection_list_controller.dart';

/// 巡检台账
class InspectionListPage extends StatelessWidget {
  InspectionListPage({super.key});

  final controller = Get.put(InspectionListController());

  //筛选按钮
  Widget _getFilter() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(() {});
  }

  /// 顶部标题栏显示
  Widget _rowTitle(String title, String? value, Color valueColor) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(value ?? '空',
            style: TextStyle(
              color: valueColor,
              fontSize: 16.sp,
            )),
        AppGaps.hGap4,
        Text(
          title,
          style: TextStyle(
            color: AppColors.font_color_blue,
            fontSize: 12.sp,
          ),
        )
      ],
    );
  }

  Widget _line() {
    return Container(
      height: 24.w,
      width: 1,
      color: Colors.white,
    );
  }

  /// 类型判断
  String _type(int i) {
    switch (i) {
      case 0:
      case 1:
        return '正常';
      case 2:
        return '漏检';
      case 3:
        return '补检';
      default:
        return '无';
    }
  }

  Color _typeColor(int i) {
    switch (i) {
      case 0:
      case 1:
        return const Color(0xFF00CE78);
      case 2:
        return const Color(0xFFFD6B6B);
      case 3:
        return const Color(0xFFFC9900);
      default:
        return Colors.grey;
    }
  }

  Widget _widget(int i) {
    return Container(
      width: 40.w,
      height: 18.w,
      margin: EdgeInsets.only(right: 10.w),
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.only(
          topRight: Radius.circular(8),
          topLeft: Radius.circular(8),
          bottomRight: Radius.circular(8),
        ),
        color: _typeColor(i),
      ),
      child: Center(
        child: Text(
          _type(i),
          style: TextStyle(color: Colors.white, fontSize: 10.sp),
        ),
      ),
    );
  }

  /// 获取列表对象-单元-事件显示
  String _getText(InsTaskDetailEntity item) {
    return '${item.riskObjectName}${item.riskUnitName == '' ? '' : '/${item.riskUnitName}'}${item.riskEventName == '' ? '' : '/${item.riskEventName}'}';
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      initState: (_) {
        controller.inspectionsCount();
      },
      builder: (controller) {
        return Scaffold(
          backgroundColor: AppColors.bg_color_grey,
          body: Container(
            padding: EdgeInsets.only(
              top: 12.w,
              left: 14.w,
              right: 14.w,
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    Expanded(
                        child: CustomSearchEnter(
                      height: 38.w,
                      hintText: '请输入任务名称',
                      text: controller.search,
                      onCallBack: (value) {
                        controller.search = value;
                        controller.refreshPage();
                      },
                    )),
                    // AppGaps.hGap10,
                    // _getFilter(),
                  ],
                ),
                AppGaps.vGap10,
                Container(
                  height: 64.w,
                  decoration: BoxDecoration(
                    color: const Color(0xFFEEF0FF),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      _rowTitle('巡检', '${controller.count?.taskTotalNum}', const Color(0xFF5C6DFF)),
                      _line(),
                      _rowTitle('正常', '${controller.count?.quantityCompleted}', const Color(0xFF00CE78)),
                      _line(),
                      _rowTitle('补检', '${controller.count?.reExaminationNum}', const Color(0xFFFC9900)),
                      _line(),
                      _rowTitle('漏检', '${controller.count?.missingNum}', const Color(0xFFFD6B6B)),
                    ],
                  ),
                ),
                AppGaps.vGap16,
                Expanded(
                    child: CustomEasyRefreshList<InsTaskDetailEntity>(
                  dataLoad: (index, size, {search}) async {
                    return controller.getListData(index, size);
                    // return [InsTaskDetailEntity()];
                  },
                  key: controller.globalKey,
                  itemBuilder: (item, index) {
                    return CustomTitleChartPage(
                      titleView: SizedBox(
                        height: 46.w,
                        child: Row(
                          children: [
                            AppGaps.hGap14,
                            SvgPicture.asset(
                              AppImages.icon_title,
                              width: 20.w,
                              height: 20.w,
                            ),
                            AppGaps.hGap6,
                            Expanded(
                              child: Text(
                                item.troubleshootContent ?? '无',
                                style: TextStyle(
                                  color: const Color(0xff262F38),
                                  height: 1.2,
                                  fontWeight: FontWeight.w500,
                                  fontSize: 14.sp,
                                ),
                              ),
                            ),
                            AppGaps.hGap8,
                            _widget(item.checkStatus ?? 0),
                            SvgPicture.asset(
                              AppImages.icon_details_right,
                              width: 16.w,
                              height: 16.w,
                            ),
                            AppGaps.hGap16,
                          ],
                        ),
                      ).click(() {
                        controller.gotoDetail(item);
                      }),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          CustomRowTitle(
                            title: '计划名称:',
                            content: item.planName ?? '',
                          ),
                          CustomRowTitle(
                            title: '巡检人：',
                            content: item.checkUserName ?? '',
                          ),
                          CustomRowTitle(
                            title: '巡检时间：',
                            content: item.checkTime ?? '',
                          ),
                          Container(
                            margin: EdgeInsets.only(left: 8.w),
                            child: Text(_getText(item),
                                style: TextStyle(
                                  fontSize: 12.sp,
                                  color: const Color(0xFF495061),
                                )),
                          ),
                          CustomRowTitle(
                            title: '管控措施：',
                            content: item.riskMeasureDesc ?? '',
                          ),
                        ],
                      ),
                    );
                  },
                ))
              ],
            ),
          ),
        );
      },
    );
  }
}
